package com.huluobo.base

import android.content.Context
import androidx.room.Room
import com.huluobo.base.db.ParkDatabase
import com.huluobo.base.db.UserDao
import com.huluobo.base.net.ApiService
import com.huluobo.base.net.ApiServiceLogin
import com.huluobo.base.net.getServiceSign
import com.huluobo.base.net.getServiceToken
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

/**
 *  Created by LC on 2024/7/24.
 *  Hilt 对外提供实例的工厂
 *  主要作用,之前写的所有的Utils,都可以在当前类得到实现
 *  直接对外提供方法,不需要再通过Utils来提供
 */
@Module
@InstallIn(SingletonComponent::class)
object ProviderFactory {
    @Provides
    @Singleton
    fun serviceSign(): ApiServiceLogin = getServiceSign()

    @Provides
    @Singleton
    fun serviceToken(): ApiService = getServiceToken()

    @Provides
    @Singleton
    fun database(@ApplicationContext app: Context): ParkDatabase =
        Room.databaseBuilder(app, ParkDatabase::class.java, "park.db")
            .fallbackToDestructiveMigration()//不需要更新加载策略
            .allowMainThreadQueries().build()

    @Provides
    @Singleton
    fun userDao(database: ParkDatabase): UserDao = database.getUserDao()
}